﻿using System;
using System.Collections.Generic;
using System.Text;
using Android.App;
using Android.Content;
using Android.Views;
using Android.Media;
using LinkNote.Model;
using Android.Widget;

namespace LinkNote.Core
{
    public class VideoPlayer
    {
        private Context context;
        private ViewGroup parent;
        private VideoView player;

        //public event DeleteClickDelegate<VideoBlock> DeleteClick;
        //public VideoBlock data;
        public event DeleteClickDelegate<AttachmentBlock> DeleteClick;
        private AttachmentBlock data;
        private ImageButton btnClose;

        public VideoPlayer(Context context, ViewGroup parent, AttachmentBlock fileName)
        {
            this.context = context;
            this.parent = parent;
            this.data = fileName;

            LayoutInflater li = LayoutInflater.FromContext(context);
            View v = li.Inflate(Resource.Layout.videoplayer, parent, false);

            //Get references to the child controls
            Button btnPlay = (Button)v.FindViewById(Resource.Id.btnPlay);
            player = (VideoView)v.FindViewById(Resource.Id.contentView);
            btnClose = v.FindViewById<ImageButton>(Resource.Id.btnClose);
            btnClose.Click += (sender, e) =>
            {
                //attachment.Visibility = ViewStates.Gone;
                if (data != null)
                    DBHelper.sqlitedb.Delete(data);
                parent.RemoveView(v);
                DeleteClick(sender, data);
            };
            parent.AddView(v);

            btnPlay.Enabled = false;
            if (string.IsNullOrEmpty(data.file) || !System.IO.File.Exists(data.file))
                CoreHelper.DownLoadFile(data, delegate { btnPlay.Enabled = true; });
            else
                btnPlay.Enabled = true;
            player.Completion += delegate(object sender, EventArgs args)
            {
                //btnPlay.Text = "播放";
                btnPlay.SetBackgroundResource(Resource.Drawable.play);
            };
            player.Click += (s, e) =>
                {
                    player.StopPlayback();
                    btnPlay.SetBackgroundResource(Resource.Drawable.play); ;

                    var dlg = new VideoPlayerDialog(context, data.file);
                    dlg.Show();
                };

            btnPlay.Click += (s, e) =>
            {
                if (!player.IsPlaying)
                {
                    var uri = Android.Net.Uri.Parse(data.file);
                    player.SetVideoURI(uri);
                    player.Start();
                    //btnPlay.Text = "停止";
                    btnPlay.SetBackgroundResource(Resource.Drawable.audio);
                }
                else
                {
                    player.StopPlayback();
                    //btnPlay.Text = "播放"
                    btnPlay.SetBackgroundResource(Resource.Drawable.play); ;
                }
            };
        }

        public bool Editable
        {
            get { return btnClose.Visibility == ViewStates.Visible; }
            set
            {
                btnClose.Visibility = value ? ViewStates.Visible : ViewStates.Invisible;
            }
        }
    }

    public class VideoPlayerDialog : Dialog
    {
        public VideoPlayerDialog(Context context, string localFile)
            : base(context)
        {
            VideoView vv = new VideoView(context);
            ViewGroup.LayoutParams param = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FillParent, ViewGroup.LayoutParams.FillParent);
            this.AddContentView(vv, param);
            var uri = Android.Net.Uri.Parse(localFile);
            vv.SetVideoURI(uri);
            vv.Start();
        }
    }
}
